package com.tihi.util;

import android.R.integer;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.tihi.handsuiviewer.App;
import com.tihi.handsuiviewer.AppDef.PreferenceDef;

/**
 * 
 * @author duantihi
 *
 * @date 2016-2-17 下午4:47:06
 */
public class Preferences {
	private static Preferences userInfo;
	private SharedPreferences preferences;
	
	private Preferences() {
		preferences = App.instance().getSharedPreferences(PreferenceDef.APP_PREFERENCE_TABLE, 0);
	}

	public static Preferences instance() {
		if (userInfo == null)
			userInfo = new Preferences();
		return userInfo;
	}

	public String getString(String key) {
		return preferences.getString(key, "");
	}
	
	public void setString(String key, String value){
		Editor edit = preferences.edit();
		edit.putString(key, value);
		edit.commit();
	}

	public SharedPreferences getSharedPreferences() {
		return preferences;
	}
	
	public Editor getEditor(){
		return preferences.edit();
	}
	public void clearAll() {
		preferences.edit().clear().commit();
	}
	
	public boolean remove(String key){
		return preferences.edit().remove(key).commit();
	}
	
	/**
	 * 保存一个实体，类名为key
	 * */
	public void saveObject(Object obj) {
		if(obj==null)
			return;
		try{
		String value=JSON.toJSONString(obj);
		Editor e=preferences.edit();
		e.putString(obj.getClass().getName(), value);
		e.commit();
		}catch(Exception e){
			
		}
	}
	
	/**
	 * 保存一个实体
	 * @param key
	 * @param obj
	 */
	public void saveObject(String key, Object obj) {
		if(obj==null)
			return;
		String value = null;
		try {
			value=JSON.toJSONString(obj);
		} catch (Exception e){
			e.printStackTrace();
		}
		if(TextUtils.isEmpty(value)){
			return;
		}
		Editor e=preferences.edit();
		e.putString(key, value);
		e.commit();
	}
	
	/**
	 *获取一个存储实体
	 * */
	public <T> T getObject(Class<T> c) {
		String value = preferences.getString(c.getName(), "");
		if (!StringUtils.isNotEmpty(value))
			return null;
		try {
			return JSON.parseObject(value, c);
		} catch (Exception e) {

		}
		return null;
	}
	

	/**
	 * 获取一个存储实体
	 * @param key 
	 * @param c
	 * @return
	 */
	public <T> T getObject(String key, Class<T> c) {
		String value = preferences.getString(key, "");
		if (!StringUtils.isNotEmpty(value))
			return null;
		try {
			return JSON.parseObject(value, c);
		} catch (Exception e) {

		}
		return null;
	}
	
	public boolean hasKey(String key) {
		return preferences.contains(key);
	}
	
	//广告显示时间保存
	private static final String ANA_ACT_AD_SHOW_TIME = "anaActAdShowTime";
	private static final String FLOAT_TIP_VIEW_SHOW_CNT = "floatTipViewShowCnt";
	
	//新手指引
	private static final String NEWER_GUIDE_IS_SHOW = "newerGuideIsShow";
	
	public void saveAdShowCurTime() {
		Editor edit = preferences.edit();
		edit.putLong(ANA_ACT_AD_SHOW_TIME, System.currentTimeMillis());
		edit.commit();
	}
	
	/**
	 * ad是否是1天前显示的
	 * @return
	 */
	public boolean adShowBeforeOneDay() {
		long lastTime = preferences.getLong(ANA_ACT_AD_SHOW_TIME, System.currentTimeMillis());
		long betweenDays = (long)((System.currentTimeMillis() - lastTime) / (1000 * 60 * 60 *24)); 
		return betweenDays >= 1;
	}
	
	/**
	 * 保存悬浮窗提示语显示次数
	 */
	public void saveFloatTipViewShowCnt() {
		int showedCnt = preferences.getInt(FLOAT_TIP_VIEW_SHOW_CNT, 0);
		showedCnt++;
		Editor editor = preferences.edit();
		editor.putInt(FLOAT_TIP_VIEW_SHOW_CNT, showedCnt);
		editor.commit();
	}
	
	/**
	 * 是否应该显示悬浮窗提示语，
	 * 最多显示3次
	 * @return
	 */
	public boolean shouldShowFloatTip() {
		int showedCnt = preferences.getInt(FLOAT_TIP_VIEW_SHOW_CNT, 0);
		if(showedCnt >= 3) {
			return false;
		}else {
			return true;
		}
	}
	
	/**
	 * 设置新手指引显示了
	 */
	public void setGuideShowed() {
		Editor edit = preferences.edit();
		edit.putBoolean(NEWER_GUIDE_IS_SHOW, true);
		edit.commit();
	}
	
	/**
	 * 新手指引是否显示过了
	 * @return
	 */
	public boolean getGuideShowed() {
		return preferences.getBoolean(NEWER_GUIDE_IS_SHOW, false);
	}
}
